# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.pro>

_realname=texstudio
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=4.4.1
pkgrel=1
pkgdesc="Integrated writing environment for creating LaTeX documents (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url="http://texstudio.sourceforge.net/"
license=('spdx: GPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-poppler-qt6"
         "${MINGW_PACKAGE_PREFIX}-quazip-qt6"
         "${MINGW_PACKAGE_PREFIX}-hunspell"
         "${MINGW_PACKAGE_PREFIX}-qt6-svg"
         "${MINGW_PACKAGE_PREFIX}-qt6-declarative"
         "${MINGW_PACKAGE_PREFIX}-qt6-5compat")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools")
optdepends=("${MINGW_PACKAGE_PREFIX}-evince: pdf reader")
source=("${_realname}-$pkgver.tar.gz::https://github.com/texstudio-org/texstudio/archive/${pkgver}.tar.gz"
        '0001-install-extra-files-on-mingw.patch'
        '0002-texstudio-4.2.2-fallbackpaths-win32-as-unix.patch'
        '0003-texstudio-4.2.3-fix-cast-to-smaller-type.patch')
sha256sums=('83c46c971081c053a01a186b4a7fbb492d75bef4070ff8fa045311eda186562b'
            'b89f798f910e54dc6618edc09ab2834537897b5b18e0590fcb75c3530d4796fb'
            '50695b93a29f9d0078d9a46e281a2366cae36d78d4ebd54e835151041fe7d359'
            '0fa4e827ac09d68921b6b647a10f12030edda85879bd14b68de7ecc427a0d131')

prepare() {
    cd ${srcdir}/${_realname}-${pkgver}

    patch -Np1 -i "${srcdir}"/0001-install-extra-files-on-mingw.patch
    patch -Np1 -i "${srcdir}"/0002-texstudio-4.2.2-fallbackpaths-win32-as-unix.patch
    patch -Np1 -i "${srcdir}"/0003-texstudio-4.2.3-fix-cast-to-smaller-type.patch
}

build() {
    mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

    declare -a _extra_config
    if check_option "debug" "n"; then
        _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
    else
        _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
    fi

    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
        -GNinja \
        -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
        "${_extra_config[@]}" \
        ../${_realname}-${pkgver}

    ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
    cd "${srcdir}/build-${MSYSTEM}"

    DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

    install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
